package br.edu.utfpr.backupTools.app;

import br.edu.utfpr.backupTools.util.BackupToolsPropriedades;
import br.edu.utfpr.backupTools.util.Configuracoes;
import br.edu.utfpr.backupTools.util.enums.Acao;
import br.edu.utfpr.backupTools.view.action.strategy.Control;
import org.apache.log4j.ConsoleAppender;
import org.quartz.SchedulerException;

import br.edu.utfpr.backupTools.model.pojo.Configuracao;
import br.edu.utfpr.backupTools.util.quartz.QuartzConfigure;
import br.edu.utfpr.backupTools.view.telas.principal.JanelaPrincipal;

import javax.swing.*;
import java.lang.invoke.MethodHandles;
import java.util.List;

/**
 * Classe main da aplicação
 * 
 * Chama a janela principal e configura o Quartz com o horario da configuração ativa.
 * 
 * @author Luan Malaguti Reffatti
 *
 */
public class Aplication {
	static QuartzConfigure quartzConfigure;
	
	public static void main(String[] args) {
		try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
       
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    defineLocais();
                    new JanelaPrincipal().setVisible(true);
                    agendaBackups();
				} catch (SchedulerException e) {
					e.printStackTrace();
				}
            }
        });
	}
	
	public static void agendaBackups() throws SchedulerException
	{
        quartzConfigure = new QuartzConfigure();

        if (Control.getInstance().listCommand().get(Acao.LISTAR_CONFIGURACAO).execute().isEmpty()) {
            quartzConfigure.configure(15, 30);

            Configuracao c = new Configuracao();
            c.setId(1L);
            c.setHora(15);
            c.setMinutos(30);
            c.setIp("localhost");
            c.setValidator(0);

            Control.getInstance().command().get(Acao.SALVAR_CONFIGURACAO).execute(c);
            return;
        }

        Configuracao con = (Configuracao) Control.getInstance().listCommand().get(Acao.LISTAR_CONFIGURACAO).execute().get(0);
        quartzConfigure.configure(con.getHora(), con.getMinutos());

        if(validador()){
            System.out.println("Licença invalida!");
            System.exit(0);
        }
	}
	
	public static void agendaBackups(Configuracao conf,boolean log){
        quartzConfigure.reConfigure(conf.getHora(), conf.getMinutos(),log);
	}

    public static void defineLocais(){
        try {
            System.out.println(System.getProperty("user.home")+"/BackupTools");
            BackupToolsPropriedades.getInstance().set(Configuracoes.HOST_BANCO,System.getProperty("user.home")+"/BackupTools");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean validador(){
        List<Configuracao> confs = Control.getInstance().listCommand().get(Acao.LISTAR_CONFIGURACAO).execute();
        Configuracao conf = confs.get(0);
        conf.setValidator(conf.getValidator() + 1);
        Control.getInstance().command().get(Acao.SALVAR_CONFIGURACAO).execute(conf);
        System.out.println(conf.getValidator());
        return conf.getValidator() >= 50;
    }
}
